<?php
class class_gd
{
	use trait_gdtext;
	use trait_gdimg;
	use trait_gdcolor;
	use trait_gdpx;
	
	var $sw;
	var $sh;
	var $sx;
	var $sy;

	var $handle;
		
	var $type = 'png';
	var $mime = 'image/png';
	var $int = IMAGETYPE_PNG;
	var $func = 'imagepng';
	
//-------------------------------------------------------------------------------

	function __construct($mixed = '')
	{		
		if(!empty($mixed)) $this->init($mixed);
	}
//-------------------------------------------------------------------------------

	function initwh($w, $h, $color = 'ffffff')
	{
		$obj = $this->makewh($w, $h, $color);
		$this->init($obj);
	}
//-------------------------------------------------------------------------------

	function initStr($str)
	{
		$srcInfo = $this->_getimagesizefromstring($str);
    if($srcInfo === false) return false;
    
    $this->sw = (float)$srcInfo[0];
    $this->sh = (float)$srcInfo[1];
    $this->settype($srcInfo[2]);
    $this->handle = imagecreatefromstring($str);
    $this->sx = $this->sw - 1;
    $this->sy = $this->sh - 1;
    return $this;
	}
//-------------------------------------------------------------------------------

	function initfile($filename)
	{
    $srcInfo = getimagesize($filename);

    if($srcInfo === false) return false;
    
    $this->sw = (float)$srcInfo[0];
    $this->sh = (float)$srcInfo[1];
    $this->settype($srcInfo[2]);
    $this->sx = $this->sw - 1;
    $this->sy = $this->sh - 1;
    switch($srcInfo[2])
    {
      case 1 :
					$this->handle = imagecreatefromgif($filename);
					break;
      case 2 :
					$this->handle = imagecreatefromjpeg($filename);
					break;
      case 3 :
					$this->handle = imagecreatefrompng($filename);
					break;
			default:
					$this->handle = null;
					break;
    }
    
    return $this;
	}
//-------------------------------------------------------------------------------

	function init($mixed)
	{
		$this->all = array();
		
		if($mixed == '')
		{
			return false;
		}
		else if(is_resource($mixed))
		{
			$this->handle = $mixed;
			$this->sh = (int)imagesy($mixed);
			$this->sw = (int)imagesx($mixed);
    	$this->sx = $this->sw - 1;
    	$this->sy = $this->sh - 1;
    	return $this;
			//不用设置类型了
		}
		else if(is_object($mixed))
		{
			$this->handle = $mixed->handle;
			$this->sh = $mixed->sh;
			$this->sw = $mixed->sw;
    	$this->sx = $this->sw - 1;
    	$this->sy = $this->sh - 1;
			$this->settype($mixed->int);
			return $this;
		}
		else if(is_array($mixed))	//点阵
		{
			$w = count($mixed);
			$h = count($mixed[0]);
			$im = imagecreatetruecolor($w, $h);
			$this->init($im);
			$this->all = $mixed;
			$this->reDrow();
		}
		else if($mixed{255} != '')
		{
			return $this->initStr($mixed);
		}
		else if(_fh($mixed))
		{
			return $this->initfile($mixed);
		}
		else
		{
			return false;
		}
	}
//-------------------------------------------------------------------------------

	function __destruct()
	{
	}
//-------------------------------------------------------------------------------

	function makewh($w = '', $h = '', $color = 'ffffff')
	{
		if($w == '') $w = $this->sw;
		if($h == '') $h = $this->sh;
		
		if($w < 1 || $h < 1) return false;
		
		$im = imagecreatetruecolor($w, $h);
		$obj = $this->make($im);
		if($color) $obj->fill(0, 0, $w, $h, $color);
		return $obj;
	}
//-------------------------------------------------------------------------------

	function make($im)
	{
		$class = get_class($this);
		return NEW $class($im);
	}
//-------------------------------------------------------------------------------
	function cloneme()
	{
		return $this->makeme();
	}
//-------------------------------------------------------------------------------

	function makeme()
	{
		return $this->make($this->handle);
	}
	// --------------------------------------------------------------------
	function savejpg($filename, $int = 75)
	{
		imagejpeg($this->handle, $filename, $int);
	}
	// --------------------------------------------------------------------
	
	function save($filename, $type = '')
	{
		if($type != '') $this->settype($type);
		$func = $this->func;
		$func($this->handle, $filename);
	}
	// --------------------------------------------------------------------
	function getstr($type = '')
	{
		return $this->getPicStream($type);
	}
	// --------------------------------------------------------------------
	function getPicStream($type = '')
	{
		ob_start();
		
		$this->echoPic($type, false);
		$str = ob_get_contents();
		ob_end_clean();
		
		$str = trim($str);
		if($str{0} === 'W' && $str{1} === 'a')
		{
			$str = preg_replace('~[\s]*Warning\: Cannot modify header information.+[\s]+~', '', $str, 1);
		}
		
		return $str;
	}
	// --------------------------------------------------------------------
	function echoPic($type = '', $header = true)
	{
		if($type != '')
			$this->settype($type);
				
		if($header)
			header("Content-Type: {$this->mime}");
		
		$func = $this->func;
		$func($this->handle);
	}
	// --------------------------------------------------------------------
	function ePic($handle)
	{
		header("Content-Type: {$this->mime}");
		imagepng($handle);
	}
	// --------------------------------------------------------------------
	function settype($type)
	{
	//GD库没有bmp输出,所以只能读入,不能输出
		static $funcs = array(
			IMAGETYPE_PNG => 'imagepng',
			IMAGETYPE_GIF => 'imagegif',
			IMAGETYPE_JPEG => 'imagejpeg',
		);

		static $ints = array(
			'gif' => IMAGETYPE_GIF,
			'jpg' => IMAGETYPE_JPEG,
			'jpeg' => IMAGETYPE_JPEG,
			'png' => IMAGETYPE_PNG,
		);

		if(is_numeric($type))
		{
			$this->int = (int)$type;
		}
		else
		{
			$type = strtolower($type);
			$this->int = $ints[$type];
		}
		
		$this->mime = image_type_to_mime_type($this->int);
		$this->type = image_type_to_extension($this->int, false);
		if(isset($funcs[$this->int])) $this->func = $funcs[$this->int];
	}
	// --------------------------------------------------------------------
	function _getimagesizefromstring($str)
	{
		if(function_exists('getimagesizefromstring'))
		{
			return getimagesizefromstring($str);
		}
		else
		{
			$path = _ftmp();
			_fs($path, $str);
			return getimagesize($path);
		}
	}
}